package com.example.day1203_newsport.proveder;

import com.example.day1203_newsport.ResourceTable;
import com.example.day1203_newsport.bean.NewsType;
import com.example.day1203_newsport.utils.CommonUtils;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.List;

/**
 * 新闻分类列表
 */
public class NewsTypeList extends BaseItemProvider {

    private List<NewsType> list;
    private Context context;

    public NewsTypeList(List<NewsType> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        ViewHolder viewHolder;
        Component component1 = component;
        if (component1 == null) {
            viewHolder = new ViewHolder();
            // 找到关联布局
            component1 = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item_news_type_list, null, false);
            // 找到关联组件
            viewHolder.typyText = component1.findComponentById(ResourceTable.Id_text_type_item);
            // 将组件关联布局
            component1.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) component1.getTag();
        }

        NewsType newsType = list.get(i);
        viewHolder.typyText.setText(newsType.getName());

        if (newsType.isSelect()){
            viewHolder.typyText.setTextColor(new Color(CommonUtils.getColor(context,ResourceTable.Color_blue)));
            viewHolder.typyText.setScaleX(1.2f);
            viewHolder.typyText.setScaleY(1.2f);
        } else {
            viewHolder.typyText.setTextColor(new Color(CommonUtils.getColor(context,ResourceTable.Color_grey)));
            viewHolder.typyText.setScaleX(1.0f);
            viewHolder.typyText.setScaleY(1.0f);
        }

        return component1;
    }


    /**
     * 组件容器
     */
    private class ViewHolder {
        Text typyText;
    }


}
